#include "HixlMemStore.h"
#include <cstring>
namespace hixl{
  HixlMemStore::HixlMemStore() {
  }

  HixlMemStore::~HixlMemStore() {
  }

  HcclResult HixlMemStore::RegisterClientMemory(void* addr, size_t size) {
    if (addr == nullptr || size == 0) {
      return HCCL_E_PARA;
    }

    client_region_ = MemoryRegion(addr, size);
    return HCCL_SUCCESS;
  }

    HcclResult HixlMemStore::RegisterServerMemory( void* addr, size_t size) {
    if (addr == nullptr || size == 0) {
      return HCCL_E_PARA;
    }

    server_region_ = MemoryRegion(addr, size);
    
    return HCCL_SUCCESS;
  }

  HcclResult HixlMemStore::UnregisterServerMemory() {
    server_region_ = MemoryRegion();
    return HCCL_SUCCESS;
  }

  HcclResult HixlMemStore::UnregisterClientMemory() {
      server_region_ = MemoryRegion();
      return HCCL_SUCCESS;
    }

    std::lock_guard<std::mutex> lock(client_mutex_);
    
    // 查找并删除对应的内存区域
    for (auto it = client_regions_.begin(); it != client_regions_.end(); ++it) {
      // 查找并删除对应的内存区域
      // 简化实现，实际需要根据具体数据结构来查找
    }
    
    return HCCL_SUCCESS;
  }

  HcclResult HixlMemStore::ValidateMemoryAccess(void* server_addr, size_t server_size, 
                                    void* client_addr, size_t client_size) {
    if (server_addr == nullptr || client_addr == nullptr || 
        server_size == 0 || client_size == 0) {
      return HCCL_E_PARA;
    }

    bool server_valid = false;
    bool client_valid = false;

    // 验证Server端内存访问
    {
      std::lock_guard<std::mutex> lock(server_mutex_);
      for (const auto& region : server_regions_) {
        if (region.second.Contains(server_addr, server_size)) {
        server_valid = true;
        break;
        }
      }
    }

    // 验证Client端内存访问
    {
      std::lock_guard<std::mutex> lock(client_mutex_);
      for (const auto& region : client_regions_) {
        if (region.second.Contains(client_addr, client_size)) {
        client_valid = true;
        break;
        }
      }
    }

    if (!server_valid || !client_valid) {
      return HCCL_E_UNAVAIL; // 内存未注册或访问越界
    }

    return HCCL_SUCCESS;
  }
}